package com.tensquare.article.controller;

import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/23 21:23
 */
@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * fetch data by rule id
     *
     * @description  增加文章
     * @param
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Article article){
        articleService.add(article);
        return new Result(true, StatusCode.OK,"添加成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  文章全部列表
     * @param
     * @return
     */
    @GetMapping
    public Result findAll(){
        return new Result(true,StatusCode.OK,"查询成功",articleService.findAll());
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID查询文章
     * @param
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        return new Result(true,StatusCode.OK,"查询成功",articleService.findById(id));
    }

    /**
     * fetch data by rule id
     *
     * @description  修改文章
     * @param
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable String id,@RequestBody Article article){
        article.setId(id);
        articleService.update(article);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID删除文章
     * @param
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        articleService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  根据条件查询文章列表
     * @param
     * @return
     */
    @PostMapping("/search")
    public Result findSearch(@RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",articleService.findSearch(searchMap));
    }

    /**
     * fetch data by rule id
     *
     * @description  文章分页 + 条件查询
     * @param
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result findSearch(@RequestBody Map searchMap,@PathVariable int page,@PathVariable int size){
        Page<Article> pageList = articleService.findSearch(searchMap, page, size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
    }

    /**
     * fetch data by rule id
     *
     * @description  点赞
     * @param
     * @return
     */
    @PutMapping("/thumbup/{id}")
    public Result updateThumbup(@PathVariable String id){
        articleService.updateThumbup(id);
        return new Result(true,StatusCode.OK,"点赞成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  根据频道ID获取文章列表
     * @param
     * @return
     */
    @PostMapping("/channel/{channelId}/{page}/{size}")
    public Result findArticlesByChannelid(@PathVariable String channelId,@PathVariable int page,@PathVariable int size){
        Page<Article> pageList = articleService.findArticlesByChannelid(channelId, page, size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
    }

    /**
     * fetch data by rule id
     *
     * @description  根据专栏ID获取文章列表
     * @param
     * @return
     */
    @PostMapping("/column/{columnId}/{page}/{size}")
    public Result findArticlesByColumnid(@PathVariable String columnId,@PathVariable int page,@PathVariable int size){
        Page<Article> pageList = articleService.findArticlesByColumnid(columnId, page, size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
    }

    /**
     * fetch data by rule id
     *
     * @description  文章审核
     * @param
     * @return
     */
    @PutMapping("/examine/{id}")
    public Result examine(@PathVariable String id){
        articleService.examine(id);
        return new Result(true,StatusCode.OK,"操作成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  头条文章
     * @param
     * @return
     */
    @GetMapping("/top")
    public Result top(){
        return new Result(true,StatusCode.OK,"查询成功",articleService.findArticlesByIstop());
    }
}
